<template>
  <div>
    <button @click="testget">btn</button>
    <button @click="initWS">init</button>
    <button @click="ws.start()">start</button>
    <button @click="login">login</button>
    <button @click="sendMsg">sndMsg</button>
    {{ ws }}
  </div>
</template>
<script>
import WSK from '../assets/websocker.js';

export default {
  name: 'index',
  data() {
    return {
      isDev: process.env.NODE_ENV == 'development',
      ws: null,
    };
  },
  components: {},
  methods: {
    test: function() {},
    testget: function() {
      this.$api.testHasSign.test().then((res) => {
        console.log(res);
      });
    },
    initWS: function() {
      this.ws = new WSK({
        host: 'ws://192.168.11.113:8001',
        // host: 'ws://121.40.191.89:8060',
        // host: 'ws://47.111.76.92',
        strBegin: '',
        strEnd: '<EOF>',
        strHeart: 'KEEPALIVE',
        wsObj: Object.assign({ SourceCode: 5, Users_Id: '0EC7DDC942E2BD2B' }),
        beforeOpen: () => {
          return Promise.resolve(200);
        },
        afterOpen: () => {
          return Promise.resolve(202);
        },
        beforeSendHeart: (ws) => {},
        afterGetHeart: (ws) => {},
        beforeMessage: (ws, msg, e) => {
          return Promise.resolve(msg);
        },
        afterMessage: (ws, json, e) => {},
        // beforeClose: () => {
        // },
        // afterClose: () => {

        // },
        beforeError: (err) => {},
        afterClose: (err) => {
          console.log('被踢了');
        },
      });
      this.ws.start();
    },
    login: function() {
      this.ws.send({});
    },
    sendMsg: function() {
      let json = {
        msgType: 30,
        type: 0,
        msgBody: 'aaa',
      };
      this.ws.send(json);
    },
  },
  beforeCreate: function() {},
  created: function() {},
  beforeMount: function() {},
  mounted: function() {
    // this.initWS();
  },
  beforeUpdate: function() {},
  updated: function() {},
  beforeDestroy: function() {},
  destroyed: function() {},
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less"></style>
